package com.bjsxt.userCenter.users.back.cntroller;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bjsxt.userCenter.users.pojo.AAdmins;
import com.bjsxt.userCenter.users.service.IOuterService;
import com.bjsxt.userCenter.users.service.IUserService;
import com.bjsxt.usercenter.common.controller.BaseController;
import com.bjsxt.usercenter.common.util.ConstatFinalUtil;
import com.bjsxt.usercenter.common.util.EncrypyUtil;

@Controller
public class NologinController extends BaseController
{

	@Resource
	private IUserService usersService;
	@Resource
	private IOuterService outerService;
	@RequestMapping("/login.htm")
	public String login()
	{
		
		ConstatFinalUtil.LOGGER.info("===========login=======");
		return "/back/login";
	}

	@RequestMapping("/loginSubmit.htm")
	public String loginSubmit(HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		
		HttpSession session = request.getSession();
		String info ="";
		String email = request.getParameter("email");
		if("".equals(email)){
			email=null;
		}
		if(email==null){
			info=ConstatFinalUtil.INFO_JSON.getString("7");
			request.setAttribute("info", info);
			return this.login();
		}
		String password = request.getParameter("password");
		String rand =request.getParameter("rand");
		String randSess =session.getAttribute("rand")+"";
		if(rand.equalsIgnoreCase(randSess)){
			Map<String, Object> condMap=new HashMap<String, Object>();
			condMap.put("email", email);
			AAdmins admins =this.usersService.findOneAdminsService(condMap);
			EncrypyUtil enc = new EncrypyUtil();
			if(admins !=null){
				if(enc.checkPass(password, admins.getPassword())){
					if(admins.getStatus().equals(Byte.valueOf("1"))){
						session.setAttribute("admins", admins);
						admins.setLastLoginTime(new Date());
						this.usersService.updateAdminsService(admins);
						if(this.returnUrl(request, response, admins)){
							return null;
						}else{
							return"redirect:/back/admins/adminsList.htm";
						}
						
					}else{
						info=ConstatFinalUtil.INFO_JSON.getString("6");
					}
				}else{
					info=ConstatFinalUtil.INFO_JSON.getString("5");
				}
				
			}else{
				info=ConstatFinalUtil.INFO_JSON.getString("4");
			}
		}else{
			info=ConstatFinalUtil.INFO_JSON.getString("8");
		}
		
		request.setAttribute("info", info);
		return this.login();
	}
	
	public boolean returnUrl(HttpServletRequest request,HttpServletResponse response,AAdmins admins) throws IOException{
		String returnUrl =request.getParameter("returnUrl");
		if(returnUrl==null){
			returnUrl="";
		}
		if(!"".equalsIgnoreCase(returnUrl)){
			JSONObject adminsJSON = new JSONObject();
			adminsJSON.put("name", admins.getTrueName());
			adminsJSON.put("email", admins.getEmail());
			adminsJSON.put("id", admins.getId());
			String token=UUID.randomUUID()+"";
			ConstatFinalUtil.TOOKEN_MAP.put(token, adminsJSON);
			
			if(returnUrl.indexOf("?")!=-1){
				returnUrl=returnUrl+"&token="+token;
			}else{
				returnUrl=returnUrl+"?token="+token;
			}
			
			response.sendRedirect(returnUrl);
			return true;
		}
		return false;
	}
	
	/**
	 * 
	 * json={"version":"2","data":{"token":"255b22c1-943b-477f-bcb3-478efe382d03"}}
	 * {
			"version":"1",
			"data":{
				"token":""
			}
		}
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/verificationToken.htm",produces="text/html;charset=UTF-8")
	public String verificationToken(HttpServletRequest request){
		System.out.println("=====================");
		JSONObject resultJOSN=new JSONObject();
		String json = request.getParameter("json");
		if(json==null){
			json="";
		}
		try
		{
			JSONObject reqJSON =(JSONObject) JSON.parse(json);
			if("1".equalsIgnoreCase(reqJSON.getString("version"))){
				resultJOSN =this.outerService.validatorAdmins01Service(reqJSON);
			}else{
				resultJOSN.put("version", reqJSON.getString("version"));
				resultJOSN.put("code", "11");
				resultJOSN.put("info", "协议版本号不对");
			}
		} catch (Exception e)
		{
			resultJOSN.put("code", "11");
			resultJOSN.put("info", "上行json数据格式不正确");
		}
		return resultJOSN+"";
	}
}
